import api from './index'

export const sqlAPI = {
  // 数据库实例管理
  getDatabaseInstances: (params = {}) => {
    return api.get('/sql/database-instances/', { params })
  },

  createDatabaseInstance: (data) => {
    return api.post('/sql/database-instances/', data)
  },

  updateDatabaseInstance: (id, data) => {
    return api.put(`/sql/database-instances/${id}/`, data)
  },

  deleteDatabaseInstance: (id) => {
    return api.delete(`/sql/database-instances/${id}/`)
  },

  // SQL查询
  executeQuery: (data) => {
    return api.post('/sql/execute-query/', data)
  },

  // 获取当前用户信息
  getCurrentUserInfo: () => {
    return api.get('/user/current-user-info/')
  },

  // SQL变更请求
  getSQLRequests: (params = {}) => {
    return api.get('/sql/requests/', { params })
  },

  createSQLRequest: (data) => {
    return api.post('/sql/requests/', data)
  },

  reviewSQLRequest: (id, data) => {
    return api.post(`/sql/requests/${id}/review/`, data)
  },

  // SQL历史
  getSQLHistory: (params = {}) => {
    return api.get('/sql/history/', { params })
  },

  // 我的工单
  getMyTickets: (params = {}) => {
    return api.get('/sql/my-tickets/', { params })
  },

  getMyTicketDetail: (id) => {
    return api.get(`/sql/my-tickets/${id}/`)
  },

  withdrawTicket: (id) => {
    return api.post(`/sql/my-tickets/${id}/withdraw/`)
  },

  // 数据库连接和结构
  testDatabaseConnection: (id) => {
    return api.post(`/sql/database-instances/${id}/test-connection/`)
  },

  // 测试连接参数（不保存实例）
  testConnectionWithParams: (params) => {
    return api.post('/sql/test-connection/', params)
  },

  getDatabaseSchema: (id) => {
    return api.get(`/sql/database-instances/${id}/schema/`)
  },

  // 获取数据库列表
  getDatabaseList: (id, includeSystemDbs = false) => {
    return api.get(`/sql/database-instances/${id}/databases/`, {
      params: { include_system_dbs: includeSystemDbs }
    })
  },

  // 获取指定数据库的表列表
  getTableList: (id, database) => {
    return api.get(`/sql/database-instances/${id}/databases/${database}/tables/`)
  },

  // 获取表结构
  getTableStructure: (id, database, table) => {
    return api.get(`/sql/database-instances/${id}/databases/${database}/tables/${table}/structure/`)
  },

  // SQL分析
  analyzeSQL: (data) => {
    return api.post('/sql/analyze-sql/', data)
  }
}
